﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmPhieuPhat : DevExpress.XtraEditors.XtraForm
    {
        #region khai bao bien
        
        public string maDocGia = "";
        public string soCaBiet = "";
        TextMessageBox box = new TextMessageBox();
        PhieuPhat_BLL pp_bll = new PhieuPhat_BLL();
        PhieuYeuCau_BLL pyc_bll=new PhieuYeuCau_BLL();
        CT_PhieuPhat_BLL ct_pp_bll = new CT_PhieuPhat_BLL();
        Image image;
        int mucPhat = 0;
        int tong = 0;
        #endregion

        #region LoadForm

        public frmPhieuPhat()
        {
            InitializeComponent();
        }

        private void frmPhieuPhat_Load(object sender, EventArgs e)
        {
            Load_Data();
            LoadText();
            LoadCombo();
        }

        #endregion

        #region Cac chuc nang

        private void btn_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            grv_Data.CloseEditor();
            grv_Data.UpdateCurrentRow();
            PhieuPhat_Public pp_public = new PhieuPhat_Public();
            CT_PhieuPhat_Public ct_pp_public = new CT_PhieuPhat_Public();
            if (grv_Data.RowCount > 0)
            {
                if (CheckRow())
                {
                    //tính tổng tiền
                    tongTien();
                    //luuw phiếu phạt
                    string maPhieuPhat = box.AutoID("PP", 2, pp_bll.TaoBang("select MaPhieuPhat from PhieuPhat"));
                    pp_public.MaPhieuPhat = maPhieuPhat;
                    pp_public.MaDocGia = maDocGia;
                    pp_public.NgayLapPhieu = DateTime.Now;
                    pp_public.TongTien = tong;
                    pp_public.MaNhanVien = box.MaNhanVien;

                    pp_bll.Insert(pp_public);

                    // luu vao chi tiet
                    for (int i = 0; i < grv_Data.RowCount; i++)
                    {
                        ct_pp_public.MaPhieuPhat = maPhieuPhat;
                        ct_pp_public.SoCaBiet = grv_Data.GetRowCellValue(i, colSoCaBiet).ToString();
                        ct_pp_public.MaViPham = grv_Data.GetRowCellValue(i, colMaViPham).ToString();
                        ct_pp_public.SoNgayQuaHan = box.ToInt(grv_Data.GetRowCellValue(i, colSoNgayQuaHan));
                        ct_pp_public.MucPhat = box.ToInt(grv_Data.GetRowCellValue(i, colMucPhat));
                        ct_pp_public.ThanhTien = box.ToInt(grv_Data.GetRowCellValue(i, colThanhTien));

                        ct_pp_bll.Insert(ct_pp_public);
                    }
                    XtraMessageBox.Show("Phạt quá hạn thành công!", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ((DataTable)grd_Data.DataSource).Clear();
                }

            }
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        #endregion

        #region ham viet thêm 
        private bool CheckRow()
        {
            for (int i = 0; i < grv_Data.RowCount; i++)
            {
                if (grv_Data.GetRowCellValue(i, colMaViPham).ToString().Equals(""))
                {
                    XtraMessageBox.Show("Bạn chưa chọn Mã vi phạm!",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    return false;
                }
            }
            return true;
        }
        private int tongTien()
        {
            
            for (int i = 0; i < grv_Data.RowCount; i++)
                tong += box.ToInt(grv_Data.GetRowCellValue(i,colThanhTien));
            return tong;
        }
        private void Load_Data()
        {
            grd_Data.DataSource = pp_bll.Load_PhieuPhat_QuaHan(soCaBiet,maDocGia);
            grv_Data.BestFitColumns();
        }
        private void LoadCombo()
        {
            cboMaViPham.DataSource = ct_pp_bll.TaoBang("select MaViPham,HinhThucXuLy,MucPhat from DM_ViPham where Status=1");
            cboMaViPham.DisplayMember = "MaViPham";
            cboMaViPham.ValueMember = "MaViPham";
        }
        private void LoadText()
        {
            lblMaBanDoc.Text = maDocGia;

            DataTable dt_sinhvien = pyc_bll.Load_GhiMuon_SinhVien(maDocGia);
            DataTable dt_GiaoVien = pyc_bll.Load_GhiMuon_GiaoVien(maDocGia);
            if (dt_sinhvien.Rows.Count > 0 && dt_GiaoVien.Rows.Count == 0)
            {
                lblHoTen.Text = dt_sinhvien.Rows[0]["HoVaTen"].ToString();
                lblNgaySinh.Text = dt_sinhvien.Rows[0]["NgaySinh"].ToString();
                lblLoaiThe.Text = dt_sinhvien.Rows[0]["TenLoaiThe"].ToString();
                pbAnh.Image = ConvertToImage(dt_sinhvien);
                //soLuongDaMuon = box.ToInt(dt_sinhvien.Rows[0]["SoLuongDaMuon"]);
                //soLuongCon = box.ToInt(dt_sinhvien.Rows[0]["SoLuongDuocMuon"]);
                //slg = soLuongCon - soLuongDaMuon;
                //lblSoLuongMuon.Text = soLuongDaMuon.ToString() + "   Được Mượn Tiếp: " + slg.ToString();
                // thoiGianMuon = box.ToInt(dt_sinhvien.Rows[0]["ThoiGianMuon"]);
                
                // ngayLamThe = dt_sinhvien.Rows[0]["NgayLamThe"].ToString();
                //ngayHetHan = dt_sinhvien.Rows[0]["NgayHetHan"].ToString();
                // lblGiaTriThe.Text = "Từ Ngày: " + ngayLamThe.Substring(0, 9).ToString() + "   Đến Ngày: " + ngayHetHan.Substring(0, 9);
               
                
            }
            if (dt_sinhvien.Rows.Count == 0 && dt_GiaoVien.Rows.Count > 0)
            {
                lblHoTen.Text = dt_GiaoVien.Rows[0]["HoVaTen"].ToString();
                lblNgaySinh.Text = dt_GiaoVien.Rows[0]["NgaySinh"].ToString();
                lblLoaiThe.Text = dt_GiaoVien.Rows[0]["TenLoaiThe"].ToString();
                pbAnh.Image = ConvertToImage(dt_GiaoVien);
                
                // soLuongDaMuon = box.ToInt(dt_GiaoVien.Rows[0]["SoLuongDaMuon"]);
                //soLuongCon = box.ToInt(dt_GiaoVien.Rows[0]["SoLuongDuocMuon"]);
                // slg = soLuongCon - soLuongDaMuon;
                // lblSoLuongMuon.Text = soLuongDaMuon.ToString() + "   Được Mượn Tiếp: " + slg.ToString();
                // thoiGianMuon = box.ToInt(dt_GiaoVien.Rows[0]["ThoiGianMuon"]);
                
                //  ngayLamThe = dt_GiaoVien.Rows[0]["NgayLamThe"].ToString();
                // ngayHetHan = dt_GiaoVien.Rows[0]["NgayHetHan"].ToString();
                // lblGiaTriThe.Text = "Từ Ngày: " + ngayLamThe.Substring(0, 9) + "   Đến Ngày: " + ngayHetHan.Substring(0, 9);
                
            }

        }
        private Image ConvertToImage(DataTable d)
        {
            try
            {

                byte[] stream = (byte[])d.Rows[0]["Anh"];

                using (MemoryStream ms = new MemoryStream(stream, 0, stream.Length))
                {
                    ms.Write(stream, 0, stream.Length);
                    image = Image.FromStream(ms, true);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return image;
        }
        #endregion

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(XtraMessageBox.Show("Bạn có muốn xóa dòng đang chọn không?",box.Caption,MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
            {
            if (grv_Data.IsDataRow(grv_Data.FocusedRowHandle))
                grv_Data.DeleteSelectedRows();
            }
        }
        DataRow row;
        private void grv_Data_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            grv_Data.CloseEditor();
            grv_Data.UpdateCurrentRow();
            try
            {
                row = grv_Data.GetDataRow(e.RowHandle);
                string maViPham = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaViPham).ToString();
                DataTable dt = ct_pp_bll.TaoBang("select MucPhat from DM_ViPham where MaVipham='" + maViPham + "'");
                mucPhat = box.ToInt(dt.Rows[0]["MucPhat"]);
                row["MucPhat"] = mucPhat;
                int ngay = box.ToInt(grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle,colSoNgayQuaHan));
                int thanhtien = ngay * mucPhat;
                row["ThanhTien"] = thanhtien;
            }
            catch (Exception)
            { }
           
        } 


    }
}